package com.jiepu.daily.corejava.advanceed.part6thread;

import org.jetbrains.annotations.NotNull;

/**
 * @Description:
 * @author: xinjie.txt
 * @date: 2020/8/3  17:26
 */
public class CommunicationPractice extends Thread {

	Object lock = new Object();
	static int count;

	public CommunicationPractice(@NotNull String name) {
		super(name);
	}

	@Override
	public void run() {
		for (int i = 1; i <= 1000; i++) {
			System.out.println(getName() + ":  " + i);

			if (i == 500) {

				synchronized (lock) {
					try {
						if (count != 2) {
							count++;
							System.out.println(getName()+"--------   "+count+" arrived");
							lock.wait();
						}else {
							lock.notifyAll();
						}


					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} // syn
			} // if
		} // for
	} // run

	public static void main(String[] args) {

		CommunicationPractice t1 = new CommunicationPractice("A");
		CommunicationPractice t2 = new CommunicationPractice("B");
		CommunicationPractice t3 = new CommunicationPractice("C");

		t1.start();
		t2.start();
		t3.start();

		try {
			t1.join();
			t2.join();
			t3.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println("ALL OK  !!");


	} // main

}
